home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
SGI Hot Mix 17
/
Hot Mix 17.iso
/
HM17_SGI
/
research
/
examples
/
demo
/
demosrc
/
d_texture.pro
< prev
next >
Wrap
Text File
|
1997-07-08
|
20KB
|
609 lines
;$Id: d_texture.pro,v 1.19 1997/04/17 22:10:17 tremblay Exp $
;
; Copyright (c) 1997, Research Systems, Inc. All rights reserved.
; Unauthorized reproduction prohibited.
;
;+
; FILE:
; d_texture.pro
;
; CALLING SEQUENCE: d_texture
;
; PURPOSE:
; Shows the texture mapping.
;
; MAJOR TOPICS: texture mapping
;
; CATEGORY:
; IDL 5.0
;
; INTERNAL FUNCTIONS and PROCEDURES:
; pro texture_Event - Event handler
; pro texture_Cleanup - Cleanup
; pro d_texture - Main procedure
;
; EXTERNAL FUNCTIONS, PROCEDURES, and FILES:
; pro trackball__define - Create the trackball object
; texture.txt
; texture.tip
; pro gettips - Get the tips text structure..
; pro widtips - Create the widget text for tips.
; pro sizetips - Size the widget text for tips.
; elev.dat
; elev_t.dat
;
; REFERENCE: IDL Reference Guide, IDL User's Guide
;
; NAMED STRUCTURES:
; none.
;
; COMMON BLOCS:
; none.
;
; MODIFICATION HISTORY:
; 9/96, DD - Written.
; 10/96, DAT - New GUI.
;
;-
;----------------------------------------------------------------------------
;
;
; Purpose: Event handler.
;
pro texture_event, $
sEvent
; Quit the application using the close box.
;
if (TAG_NAMES(sEvent, /STRUCTURE_NAME) EQ $
'WIDGET_KILL_REQUEST') then begin
WIDGET_CONTROL, sEvent.top, /DESTROY
RETURN
endif
WIDGET_CONTROL, sEvent.id, GET_UVALUE= uvalue
case uvalue of
; Zoom by translating the object
;
'FNUMBER' : begin
WIDGET_CONTROL, sEvent.top, GET_UVALUE=sState, /NO_COPY
WIDGET_CONTROL, sState.wBase, SENSITIVE=0
dSlider = sEvent.value - sState.sliderVal
sState.sliderVal = sEvent.value
; Maximum translation is 12.8 * 20(slider value) /100
; in the view normalized coorrdinates.
;
sState.oSurfaceModel->Translate, 0., 0., dSlider/100.*12.8
sState.oWindow->Draw, sState.oView
WIDGET_CONTROL, sState.wBase, SENSITIVE=1
WIDGET_CONTROL, sEvent.top, SET_UVALUE=sState, /NO_COPY
end ; of FNUMBER
; Change the surface height acordingly to the slider value.
;
'FHEIGHT' : begin
WIDGET_CONTROL, sEvent.top, GET_UVALUE=sState, /NO_COPY
WIDGET_CONTROL, sState.wBase, SENSITIVE=0
dSlider = sEvent.value - sState.sliderVal
heightRatio = FLOAT(sEvent.value) / sState.heightFactor
newData = sState.z1 * heightRatio
sState.oSurface->SetProperty, DATAZ=newData
sState.oWindow->Draw, sState.oView
WIDGET_CONTROL, sState.wBase, SENSITIVE=1
WIDGET_CONTROL, sEvent.top, SET_UVALUE=sState, /NO_COPY
end ; of FHEIGHT
; Draw only the surface with the wire style.
;
'SURFACEONLY' : begin
WIDGET_CONTROL, sEvent.top, GET_UVALUE=sState, /NO_COPY
WIDGET_CONTROL, sState.wBase, SENSITIVE=0
WIDGET_CONTROL, sState.wSurfButton, SENSITIVE=0
WIDGET_CONTROL, sState.wSurfTextureButton, SENSITIVE=1
sState.oSurface->SetProperty, TEXTURE_MAP=OBJ_NEW(), $
STYLE=1
sState.oWindow->Draw, sState.oView
WIDGET_CONTROL, sState.wBase, SENSITIVE=1
WIDGET_CONTROL, sEvent.top, SET_UVALUE=sState, /NO_COPY
end ; of SURFACEONLY
; Draw surface with the texture mapping.
;
'SURFACETEXTURE' : begin
WIDGET_CONTROL, sEvent.top, GET_UVALUE=sState, /NO_COPY
WIDGET_CONTROL, sState.wBase, SENSITIVE=0
sState.oSurface->SetProperty, $
TEXTURE_MAP=sState.oImage, STYLE=2
WIDGET_CONTROL, sState.wSurfButton, SENSITIVE=1
WIDGET_CONTROL, sState.wSurfTextureButton, SENSITIVE=0
sState.oWindow->Draw, sState.oView
WIDGET_CONTROL, sState.wBase, SENSITIVE=1
WIDGET_CONTROL, sEvent.top, SET_UVALUE=sState, /NO_COPY
end ; of SURFACETEXTURE
; Reset the initial translation and orientation.
;
'RESET' : begin
WIDGET_CONTROL, sEvent.top, GET_UVALUE=sState, /NO_COPY
WIDGET_CONTROL, sState.wBase, SENSITIVE=0
sState.oSurfaceRotationModel->SetProperty, $
TRANSFORM=sState.initRotTM
sState.oSurfaceModel->SetProperty, $
TRANSFORM=sState.initSurTM
WIDGET_CONTROL, sState.wHeightSlider, $
SET_VALUE=5
WIDGET_CONTROL, sState.wZoomSlider, $
SET_VALUE=0
sState.oWindow->Draw, sState.oView
WIDGET_CONTROL, sState.wBase, SENSITIVE=1
WIDGET_CONTROL, sEvent.top, SET_UVALUE=sState, /NO_COPY
end ; of RESET
; Handle the event generated within the drawing area
;
'DRAW': begin
WIDGET_CONTROL, sEvent.top, GET_UVALUE=sState, /NO_COPY
; Expose.
;
if (sEvent.type eq 4) then begin
sState.oWindow->Draw, sState.oView
WIDGET_CONTROL, sEvent.top, SET_UVALUE=sState, /NO_COPY
RETURN
endif
; Handle trackball update
;
bHaveTransform = sState.oTrack->Update(sEvent, TRANSFORM=qmat )
if (bHaveTransform NE 0) then begin
sState.oSurfaceRotationModel->GetProperty, TRANSFORM=t
mt = t # qmat
sState.oSurfaceRotationModel->SetProperty,TRANSFORM=mt
endif
; Button press.
;
if (sEvent.type EQ 0) then begin
sState.btndown = 1B
sState.oWindow->setproperty, QUALITY=0
WIDGET_CONTROL, sState.wDraw, /DRAW_MOTION
endif
; Button motion.
;
if ((sEvent.type eq 2) and (sState.btndown eq 1b)) then begin
if (bHaveTransform) then begin
sState.oWindow->Draw, sState.oView
endif
endif
; Button release.
;
if (sEvent.type eq 1) then begin
if (sState.btndown EQ 1b) then begin
sState.oWindow->SetProperty, QUALITY=2
sState.oWindow->Draw, sState.oView
endif
sState.btndown = 0b
WIDGET_CONTROL, sState.wDraw, DRAW_MOTION=0
endif
WIDGET_CONTROL, sEvent.top, SET_UVALUE=sState, /NO_COPY
end ; of DRAW
'QUIT' : BEGIN
WIDGET_CONTROL, sEvent.top, /DESTROY
end ; of QUIT
'ABOUT' : BEGIN
; Verify that there is only one instance of XDisplayFile
;
if (Xregistered('XDisplayFile') NE 0) then RETURN
XDisplayFile, filepath("texture.txt", $
SUBDIR=['examples','demo','demotext']), $
DONE_BUTTON='Done', $
TITLE="Texture", $
GROUP=sEvent.top, WIDTH=54, HEIGHT=14
end ; of ABOUT
endcase
end
;-----------------------------------------------------------------
;
; PURPOSE : cleanup procedure. restore colortable, destroy objects.
;
pro Texture_Cleanup, wBase
WIDGET_CONTROL, wBase, GET_UVALUE=sState, /NO_COPY
; Destroy the top objects.
;
OBJ_DESTROY, sState.oView
OBJ_DESTROY, sState.oText
OBJ_DESTROY, sState.oFont
OBJ_DESTROY, sState.oImage
OBJ_DESTROY, sState.oTrack
OBJ_DESTROY, sState.oContainer
; Restore the color table.
;
TVLCT, sState.colorTable
if widget_info(sState.groupBase, /valid) then $
widget_control, sState.groupBase, /map
end ; of Texture_Cleanup
;-----------------------------------------------------------------
;
; PURPOSE : show the texture mapping capability
;
PRO d_texture, $
TEXCOORDS = texcoords, $ ; IN: (opt) texture coordinates
GROUP=group, $ ; IN: (opt) group identifier
APPTLB = appTLB ; OUT: (opt) TLB of this application
; Check the validity of the group identifier.
;
ngroup = N_ELEMENTS(group)
if (ngroup NE 0) then begin
check = WIDGET_INFO(group, /VALID_ID)
if (check NE 1) then begin
print,'Error, the group identifier is not valid'
print, 'Return to the main application'
RETURN
endif
groupBase = group
endif else groupBase = 0L
; Set up dimensions of the drawing (viewing) area.
;
device, GET_SCREEN_SIZE=scr
xdim = scr(0)*0.6
ydim = xdim*0.8
; Get the current color vectors to restore
; when this application is exited.
;
TVLCT, savedR, savedG, savedB, /GET
; Build color table from color vectors
;
colorTable = [[savedR],[savedG],[savedB]]
; Get the tips.
;
sText = getTips(filepath('texture.tip', $
SUBDIR=['examples','demo', 'demotext']) )
; Create widgets.
;
if (N_ELEMENTS(group) EQ 0) then begin
wBase = WIDGET_BASE(/COLUMN, $
TITLE="Texture Mapped Terrain", $
XPAD=0, YPAD=0, $
/TLB_KILL_REQUEST_EVENTS, $
TLB_FRAME_ATTR=1, Mbar=barBase)
endif else begin
wBase = WIDGET_BASE(/COLUMN, $
TITLE="Texture Mapping", $
GROUP_LEADER=group, $
XPAD=0, YPAD=0, $
/TLB_KILL_REQUEST_EVENTS, $
TLB_FRAME_ATTR=1, Mbar=barBase)
endelse
; Create the menu bar. It contains the file/quit,
; edit/ shade-style, help/about.
;
wFileButton = WIDGET_BUTTON(barBase, VALUE='File', /MENU)
wQuitButton = WIDGET_BUTTON(wFileButton, $
VALUE='Quit', UVALUE='QUIT')
; Offer two options : surface only , surface with texture
;
wOptionButton = WIDGET_BUTTON(barBase, VALUE='Options', /MENU)
wSurfButton = WIDGET_BUTTON(wOptionButton, $
VALUE = 'Surface Only', UVALUE='SURFACEONLY')
wSurfTextureButton = WIDGET_BUTTON(wOptionButton, $
VALUE='Surface with Texture', UVALUE='SURFACETEXTURE')
wResetButton = WIDGET_BUTTON( wOptionButton, $
VALUE='Reset', UVALUE='RESET')
; Create the menu bar item help that contains the about button
;
wHelpButton = WIDGET_BUTTON(barBase, VALUE='About', /HELP, /MENU)
wAboutButton = WIDGET_BUTTON(wHelpButton, $
VALUE='About Texture Mapping', UVALUE='ABOUT')
; Create a sub base of the top base (wBase)
;
subBase = WIDGET_BASE(wBase, COLUMN=2)
; Create the left Base that contains the functionality buttons
; Notably the object list and the option list
;
wLeftbase = WIDGET_BASE(subBase, /ALIGN_CENTER, /COLUMN)
wZoomBase = WIDGET_BASE(wLeftBase, /COLUMN)
wZoomBaseLabel = WIDGET_LABEL(wZoomBase, $
VALUE = "Zoom")
wZoomValueBase = WIDGET_BASE(wZoomBase, $
/COLUMN, /FRAME)
wSliderBase = WIDGET_BASE(wZoomValueBase, $
/COLUMN, /ALIGN_CENTER)
wFastLbl = WIDGET_LABEL(wSliderBase, $
VALUE='+', $
/ALIGN_CENTER)
wZoomSlider = WIDGET_SLIDER(wSliderBase,$
/ALIGN_CENTER, $
MINIMUM=0, MAXIMUM=20, $
/SUPPRESS_VALUE, $
UVALUE='FNUMBER', /VERTICAL)
wSlowLbl = WIDGET_LABEL(wSliderBase, $
VALUE='-', $
/ALIGN_CENTER)
wHeightBase = WIDGET_BASE(wLeftBase, /COLUMN)
wHeightBaseLabel = WIDGET_LABEL(wHeightBase, $
VALUE="Height")
wHeightValueBase = WIDGET_BASE(wHeightBase, $
/COLUMN, /FRAME)
wSlider2Base = WIDGET_BASE(wHeightValueBase, $
/COLUMN, /ALIGN_CENTER)
wPlusLbl = WIDGET_LABEL(wSlider2Base, $
VALUE='+', $
/ALIGN_CENTER)
wHeightSlider = WIDGET_SLIDER(wSlider2Base,$
/ALIGN_CENTER, $
VALUE=5, $
MINIMUM=0, MAXIMUM=10, $
/SUPPRESS_VALUE, $
UVALUE='FHEIGHT', /VERTICAL)
wMinusLbl = WIDGET_LABEL(wSlider2Base, $
VALUE='-', $
/ALIGN_CENTER)
; Create the right Base that has the drawing area
;
wRightBase = WIDGET_BASE(subBase, /COLUMN)
; Draw area.
;
wDraw = WIDGET_DRAW(wRightBase, $
GRAPHICS_LEVEL=2, $
XSIZE=xdim, YSIZE=ydim, /BUTTON_EVENTS, $
UVALUE='DRAW', $
RETAIN=0, /EXPOSE_EVENT)
; Create tips texts.
;
wStatusBase = WIDGET_BASE(wBase, MAP=0, /ROW)
nWidgets = 2
wText = LONARR(nWidgets)
widTips, wStatusBase, sText.text, XSIZE=36, $
YSIZE=3, NWIDGETS=nWidgets, wText
; Realize the base widget.
;
WIDGET_CONTROL, wBase, /REALIZE
; Returns the top level base in the appTLB keyword
;
appTLB = wBase
; Size the tips widgets.
;
sizeTips, wBase, wText, wStatusBase
WIDGET_CONTROL, wBase, SENSITIVE=0
; Grab the window id of the drawable.
;
WIDGET_CONTROL, wDraw, GET_VALUE=oWindow
; Put centerize the surface.
;
bias = -0.5
myview = [0+bias, 0+bias, 1, 1]
; Create view.
;
oView = OBJ_NEW('idlgrview', $
PROJECTION=2, EYE=3, ZCLIP=[1.5,-1.5], $
VIEWPLANE_RECT=myview, COLOR=[0,0,0])
; Create a centerd starting up text.
;
textLocation = [myview[0]+0.5*myview[2], myview[1]+0.5*myview[3]]
; Create and display the PLEASE WAIT text.
;
oFont = OBJ_NEW('IDLgrFont', 'Helvetica', SIZE=18)
oText = OBJ_NEW('IDLgrText', $
'Starting up Please wait...', $
ALIGN=0.5, $
LOCATION=textLocation, $
COLOR=[255,255,0], FONT=oFont)
; Create model.
;
oTopModel = OBJ_NEW('idlgrmodel')
oSurfaceModel = OBJ_NEW('idlgrmodel')
oSurfaceRotationModel = OBJ_NEW('idlgrmodel')
oTopModel->Add, oSurfaceModel
oSurfaceModel->Add, oSurfaceRotationModel
; Place the model in the view..
;
oView->Add, oTopModel
oTopModel->Add, oText
oWindow->Draw, oView
WIDGET_CONTROL, wBase, /HOURGLASS
; Scale the top model to fit the viewing area.
;
sct = 0.5
oTopModel->Scale, sct, sct, sct
; Surface data is read from elevation data file.
;
z = BYTARR(64,64, /NOZERO)
OPENR, lun, filepath('elevbin.dat', $
SUBDIR=['examples','demo','demodata']), $
/GET_LUN
READU, lun, z
FREE_LUN, lun
sz = SIZE(z)
maxx = sz(1) - 1
maxy = sz(2) - 1
maxz = MAX(z, MIN=minz)
; Compute coordinate conversion to normalize.
;
xs = [0+bias, 1.0/maxx]
ys = [0+bias, 1.0/maxy]
minz2 = minz - 1
maxz2 = maxz + 1
zs = [-minz2/(maxz2-minz2)+bias, 1.0/(maxz2-minz2)]
; Create texture map.
;
READ_JPEG, filepath('elev_t.jpg', $
SUBDIR=['examples','demo','demodata']), $
idata, TRUE=3
oImage = OBJ_NEW('IDLgrImage', idata, INTERLEAVE=2)
textureCoordinates = FLTARR(2,64,64)
; Scale the texture coordinates
;
for j=0,63 do begin
for i=0,63 do begin
textureCoordinates(0,i,j) = FLOAT(i)/32.0
textureCoordinates(1,i,j) = FLOAT(j)/32.0
endfor
endfor
; Create the surface object
;
oSurface = OBJ_NEW('IDLgrSurface', z, style=2, shading=1, $
COLOR=[255,255,255], TEXTURE_MAP=oImage, $
XCOORD_CONV=xs, YCOORD_CONV=ys, ZCOORD_CONV=zs)
oSurfaceRotationModel->Add, oSurface
z1 = z/2.0
oSurface->SetProperty, DATAZ=z1
if (N_ELEMENTS(texcoords) NE 0) then begin
oSurface->SetProperty, TEXTURE_COORD=textureCoordinates
end
; Create a light.
;
oLight1 = OBJ_NEW('IDLgrLight', TYPE=0, $
INTENSITY=1., COLOR=[255,255,255])
oTopModel->Add, oLight1
oLight2 = OBJ_NEW('IDLgrLight', LOCATION=[-2,2,-2], TYPE=1)
oTopModel->Add, oLight2
; Rotate to standard view for first draw.
;
oSurfaceRotationModel->Rotate, [1,0,0],-90
oSurfaceRotationModel->Rotate, [0,1,0],30
oSurfaceRotationModel->Rotate, [1,0,0],30
oSurfaceRotationModel->GetProperty, TRANSFORM=initRotTM
oSurfaceModel->GetProperty, TRANSFORM=initSurTM
; Add the trackball object for interactive change
; of the scene orientation
;
oTrack = OBJ_NEW('Trackball', [xdim/2.0, ydim/2.0], xdim/2.0)
oContainer = OBJ_NEW('IDLgrContainer')
oContainer->Add, oView
oContainer->Add, oTrack
WIDGET_CONTROL, wSurfTextureButton, SENSITIVE=0
; Create the state structure.
;
sState = { $
btndown: 0b, $ ; Mouse button 0=pressed, other: pressed
HeightFactor: 5.0, $ ; Slider value factor for height
wDraw: wDraw, $ ; Widget draw ID
OSurfaceModel: oSurfaceModel, $ ; Surface models
OSurfaceRotationModel: oSurfaceRotationModel, $
OSurface: oSurface, $ ; Surface object
OView: oView, $ ; View object
InitRotTM: initRotTM, $ ; Initial Rotation of the surface
InitSurTM: initSurTM, $ ; Surface initial transformation matrix
sliderVal: 0, $ ; Zoom slider value
WHeightSlider: wHeightSlider, $ ; Data height slider value
WZoomSlider: wZoomSlider, $ ; Zoom slider ID
WBase : wbase, $ ; top level base
WSurfTextureButton: wSurfTextureButton, $ : Surface + texture button
WSurfButton: wSurfButton, $ ; Surfaced only button
OImage: oImage, $ ; Image object
ColorTable: colorTable, $ ; Color table to restore.
Z1: z1, $ ; Data array
OTrack: oTrack, $ ; Trackball object
OText: oText, $ ; Text object
OFont: oFont, $ ; Font object
OContainer: oContainer, $ ; Container object
OWindow: oWindow, $ ; Window object
groupBase: groupBase $ ; Base of Group Leader
}
WIDGET_CONTROL, wBase, SET_UVALUE=sState, /NO_COPY
; Draw the screen
;
oTopModel->Remove, oText
oWindow->Draw, oView
WIDGET_CONTROL, wBase, SENSITIVE=1
XMANAGER, 'd_texture', wBase, $
Event_Handler='texture_event', $
/NO_BLOCK, $
CLEANUP='texture_Cleanup'
end ; of d_texture